package com.ruoyi.collection.process;

import com.alibaba.fastjson.JSON;
import com.ruoyi.bizsys.domain.RealNameAuth;
import com.ruoyi.bizsys.service.IRealNameAuthService;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.QueryWalletAmountReq;
import com.ruoyi.collection.domain.QueryWalletAmountResp;
import com.ruoyi.collection.mapper.QueryWalletAmountMapper;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.util.business.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service("queryWalletAmountPorcessor")
public class QueryWalletAmountPorcessor implements CollectionProcess {

    @Autowired
    private QueryWalletAmountMapper queryWalletAmountMapper;
    @Autowired
    private IRealNameAuthService realNameAuthService;

    @Override
    public Object process(ApiParameter req) throws Exception {
        //请求实体
        QueryWalletAmountReq reqBody = JSON.parseObject(req.getBody().toString(), QueryWalletAmountReq.class);
        BigDecimal walletAmount;
        //根据bvn查询cust信息
        RealNameAuth realNameAuthCondition = new RealNameAuth();
        realNameAuthCondition.setBvnAccount(reqBody.getCustBvn());
        realNameAuthCondition.setState(Constants.BIZ_STATUS_1);
        List<RealNameAuth> realNameAuthList = realNameAuthService.selectRealNameAuthList(realNameAuthCondition);
        if (CollectionUtils.isEmpty(realNameAuthList)) {
            log.info("无实名信息");
            return new QueryWalletAmountResp();
        }
        //实名信息
        RealNameAuth realNameAuth = realNameAuthList.get(0);
        //查询钱包余额
        walletAmount = Optional.ofNullable(queryWalletAmountMapper.queryWalletAmount(realNameAuth.getCustNo())).orElse(BigDecimal.ZERO);
        return new QueryWalletAmountResp().setWalletAmount(walletAmount);
    }
}
